﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports MySql.Data.MySqlClient

Public Class DiasCreditoDAL
    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

#Region "FuncionesUtilizables"

    Public Function existeDia(ByVal pNumDias As Integer, Optional ByVal idDias As Integer = 0) As Boolean
        Dim id As Int16 = 0
        Dim strQueryId As String = ""
        If (idDias > 0) Then
            strQueryId = " AND IdDiasCredito <> ?IdDias "
        End If
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT COUNT(*) " & _
                                            "FROM diascredito " & _
                                            "WHERE NumDias = ?NumDias " &
                                            strQueryId & ";"
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("?NumDias", pNumDias)
                    If (idDias > 0) Then
                        myComm.Parameters.AddWithValue("?IdDias", idDias)
                    End If
                    myConn.Open()
                    id = Convert.ToInt16(myComm.ExecuteScalar())
                End Using
            End Using
            Return (id > 0)
        Catch ex As Exception
            Throw
        Finally
            id = Nothing
        End Try
    End Function

    Public Function registraDiasCredito(ByVal pDiasCredito As DiasCreditoBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "INSERT INTO diascredito(NumDias, Descripcion, Status) " & _
                                            "VALUES (@NumDias, @Descripcion, @Status);"
                    myComm.CommandType = System.Data.CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@NumDias", pDiasCredito.NumDias)
                    myComm.Parameters.AddWithValue("@Descripcion", pDiasCredito.Descripcion)
                    Select Case pDiasCredito.Status
                        Case DiasCreditoBE.StatusDiasCredito.Activo
                            myComm.Parameters.AddWithValue("@Status", "A")
                        Case DiasCreditoBE.StatusDiasCredito.Inactivo
                            myComm.Parameters.AddWithValue("@Status", "I")
                        Case Else
                            myComm.Parameters.AddWithValue("@Status", "I")
                    End Select
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function actualizaDiasCredito(ByVal pDiasCredito As DiasCreditoBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE diascredito " & _
                                            "SET NumDias = @NumDias, " & _
                                                "Descripcion = @Descripcion, " & _
                                                "Status = @Status " & _
                                            "WHERE IdDiasCredito = @IdDiasCredito; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@NumDias", pDiasCredito.NumDias)
                    myComm.Parameters.AddWithValue("@Descripcion", pDiasCredito.Descripcion)
                    Select Case pDiasCredito.Status
                        Case DiasCreditoBE.StatusDiasCredito.Activo
                            myComm.Parameters.AddWithValue("@Status", "A")
                        Case DiasCreditoBE.StatusDiasCredito.Inactivo
                            myComm.Parameters.AddWithValue("@Status", "I")
                        Case Else
                            myComm.Parameters.AddWithValue("@Status", "I")
                    End Select
                    myComm.Parameters.AddWithValue("@IdDiasCredito", pDiasCredito.IdDiasCredito)
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getDiasCredito(ByVal IdDiasCredito As Integer) As DiasCreditoBE
        Dim tmpDiasCredito As DiasCreditoBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT IdDiasCredito, NumDias, Descripcion, Status " & _
                                            "FROM diascredito " & _
                                            "WHERE IdDiasCredito = @IdDiasCredito;"
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddWithValue("@IdDiasCredito", IdDiasCredito)
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpDiasCredito = DiasCreditoFact.Build(myDR)
                        Else
                            tmpDiasCredito = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpDiasCredito

        Catch ex As Exception
            Throw
        Finally
            tmpDiasCredito = Nothing
        End Try
    End Function

    Public Function getListadoDiasCredito() As List(Of DiasCreditoBE)
        Dim DiasCreditoLs As List(Of DiasCreditoBE) = New List(Of DiasCreditoBE)
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT IdDiasCredito, NumDias, Descripcion, Status " & _
                                           "FROM diascredito;"
                    myComm.CommandType = CommandType.Text
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        While myDR.Read()
                            Dim DiasCredito As DiasCreditoBE = DiasCreditoFact.Build(myDR)
                            If (Not DiasCredito Is Nothing) Then
                                DiasCreditoLs.Add(DiasCredito)
                            End If
                            DiasCredito = Nothing
                        End While
                    End Using
                End Using
            End Using
            Return DiasCreditoLs
        Catch ex As Exception
            Throw
        Finally
            DiasCreditoLs = Nothing
        End Try
    End Function

#End Region

End Class
